<?php
/**
 * Registration page controller
 *
 * @author den
 *
 */

class RegistrationController extends modules_default_controllers_ControllerBase
{
	protected $_bForLoggedUsersOnly = false;

//----------------------------------------------------------------------------------------------------

	public $ajaxable = array(
	);

//----------------------------------------------------------------------------------------------------


	public function init()
	{
		if (! $this->getRequest()->isXmlHttpRequest())
		{
			parent::init();

			$this->view->title = 'Registration';
		}
		$this->view->headLink()->appendStylesheet('/css/validation_css/screen.css');

		$this->view->headScript()->appendFile('/js/registration.js');
		$this->view->headScript()->appendFile('/js/jquery.validate.js');

		$this->_helper->AjaxContext()->initContext('json');
	}

//----------------------------------------------------------------------------------------------------


	public function indexAction()
	{
		$oRequest = $this->getRequest();

		if (Lemar_User::isLogged())
		{
			$this->_redirect('/');
		}

		$sRedirectUrl = '/';
		$mJsonValidateReturn = '';

		if ($oRequest->isPost())
		{
			$oForm = new modules_default_forms_RegistrationForm();
			if ($oForm->isValid($oRequest->getParams()))
			{
				if (Lemar_User::doRegister($oForm, $oRequest->getPost()))
				{
					$this->_redirect('/registration/congratulation/');
				}
				else
				{
					$mJsonValidateReturn = Lemar_User::getErrorMessages();
				}
			}
			else
			{

				$aValues 	= $oRequest->getParams();
				$this->view->aValues = $aValues;

				$aErrors 	= $oForm->getErrors();
				$aMessages  = $oForm->getMessages();

//				print_r($aErrors);
//				print_r($aMessages);

				$aErrorMessages = array();

				foreach ($aErrors as $sControlName => $sErrorIndexes)
				{
					foreach($sErrorIndexes as $ErrorName)
					{
						$aErrorMessages[$sControlName][$ErrorName] = $aMessages[$sControlName][$ErrorName] ;
					}
				}
				$this->view->aError = $aErrorMessages;
				$this->view->aValues = $aValues;
			}
		}
		else
		{
//			die('else');
			//$this->view->findBy = models_FindByMapper::fetchAll();
		}

		$this->view->jsonValidateReturn = $mJsonValidateReturn;
		$this->view->redirectUrl = $sRedirectUrl;
	}

//----------------------------------------------------------------------------------------------------


} //  CLASS
